
import database.Restaurant;
import java.awt.CardLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * editpanel.java
 *
 * Created on 30 Ιαν 2012, 2:00:21 πμ
 */
/**
 *
 * @author karolos
 */
public class editpanel extends javax.swing.JPanel {
   
    DBManager db = mainframe.getDB();
    ResultSet rs;
    static Restaurant r1; //estiatorio gia enimerosi poy 8a perasei sto insertpanel
    /** Creates new form editpanel */
    public editpanel() {  
        initComponents();
      mainframe.showrestaurants(rs,jTable1);  //pros8iki estiatorion sto jtable
  
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setLayout(new java.awt.CardLayout());

        jPanel4.setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(70, 35));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Τροποποίηση Εστιατορίου");
        jPanel1.add(jLabel1);

        jPanel4.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(new java.awt.BorderLayout());

        jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));

        jTable1.setAutoCreateRowSorter(true);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5"
            }
        ));
        jTable1.setInheritsPopupMenu(true);
        jTable1.setSelectionForeground(new java.awt.Color(0, 0, 0));
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel4.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(70, 35));

        jButton1.setText("Τροποποίηση");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);

        jPanel4.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        add(jPanel4, "editpanel");
    }// </editor-fold>//GEN-END:initComponents
   public static Restaurant getrestaurant(){
   return r1;
   }

    //emfanish epilegmenou estiatoriou apo jtable sta pedia kai dhmioyrgia antikeimenoy restaurant
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    CardLayout cl1 = (CardLayout)this.getLayout();
    insertpanel ip=new insertpanel();
    ip.getbutton1().setVisible(false);
    ip.getbutton4().setVisible(true);
    this.add("insertpanel", ip);
    cl1.show(this, "insertpanel");
    ArrayList<String>al=new ArrayList<String>();
    for(int i=0;i<jTable1.getColumnCount();i++){
     al.add((String) jTable1.getValueAt(jTable1.getSelectedRow(),i));
    
    }
    //eisagogi timon sta pedia apo resultset
   ResultSet rs1=db.getrestaurantfromtable(al.get(0),al.get(1),al.get(2),al.get(3),al.get(4));
        try {
            while (rs1.next()) {
                ip.getRestname().setText(rs1.getString(1));
                ip.getRestaddress().setText(rs1.getString(2));
                ip.getRestarea().setSelectedItem(rs1.getString(3));
                ip.getRestphonenumber().setText(rs1.getString(4));
                ip.getRestcategory().setSelectedValue(rs1.getString(5), true);
                ip.getRestinfo().setText(rs1.getString(6));
                ip.getRestdescription().setText(rs1.getString(7));
                ip.getRestpricerange().setSelectedItem(rs1.getString(8));
                if(rs1.getString(9)!=null){
                ip.setImagepath(rs1.getString(9));
                BufferedImage img= ImageIO.read(new File(rs1.getString(9)));
                ip.setOriginalimage(img);
                Image imagenew=img.getScaledInstance(80, 60, WIDTH);
                ip.getjXImagePanel1().setImage(imagenew);
                }
               
            }
            
           r1=ip.createRestaurant();
           r1.setAid(db.getareaid((String)ip.getRestarea().getSelectedItem()));
           r1.setCid(db.getcatid((String)ip.getRestcategory().getSelectedValue()));
           r1.setPid(db.getpriceid((String)ip.getRestpricerange().getSelectedItem()));
           
        } catch (IOException ex) {
            Logger.getLogger(editpanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(editpanel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    



}
